package com.loginlib.table.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.loginlib.R;


/**
 * Created by Administrator on 2016-07-19.
 */
public class TableAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private String[] items;

    private int clickTemp = -1 ;//标识选择的Item

    public TableAdapter(Context context, String[] items) {
        mInflater = LayoutInflater.from(context);
        this.items = items;
    }

    public void setSelection(int position) {
        clickTemp = position;
    }

    public void setCancele(int no) {
        clickTemp = no;
    }

    @Override
    public int getCount() {
        return items.length;
    }

    @Override
    public Object getItem(int position) {
        return items[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.table_items, null);
            holder.textView = (TextView) convertView.findViewById(R.id.tv_num);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
//        点击改变选中的背景色
        if (clickTemp == position) {
            holder.textView.setBackgroundResource(R.drawable.selected_table_bg);
        } else {
            holder.textView.setBackgroundResource(R.drawable.normal_table_bg);
        }

        if (clickTemp == -2) {
            holder.textView.setBackgroundResource(R.drawable.normal_table_bg);
        }

        holder.textView.setText(items[position]+"人");
        return convertView;
    }

    public final class ViewHolder {
        TextView textView;
    }



}
